/**
 * @name 公用方法.ts
 *
 * @author yangliu at 2020-02-07
 * */
import { xhrGet, api } from '@/services/index';
import { NODE_TYPE_OBJ } from './dic';

// 新增的验证
const validAdd = (formData: any, params: any) => {
  let valid = true;
  params.forEach((item: any) => {
    if (!formData[item.key]) {
      valid = false;
    }
  });
  return { valid };
};
// 字典查询
const queryDictValueList = (attrCode: string) => xhrGet(api.dictValueList, { code: attrCode }).then((res: any) => {
    if (res.success) {
      const list = res.result.map((e: any) => ({ text: e.name, value: e.code }));
      return list;
    }
  });

// 转换data的值
const transfer = (data: any) => (
    data &&
    data.map((item: any) => ({
        id: item.id,
        isLeaf: item.isLeaf,
        code: item.code,
        name: item.name,
        nodeName: `${item.name}-${item.code}`,
        nodeType: item.type,
        parentId: item.parentId,
        typeName: NODE_TYPE_OBJ[item.type].name,
        isRoot: false,
        children: null,
      }))
  );
export { validAdd, queryDictValueList, transfer };
